<script>
	var url;
	$(document).ready(function(){
	
		newData = function (){
			$('#dialog').dialog({
				title: 'Tambah Data Keluarga',
				width: 550,
				height: $(window).height()*0.97,
				closed: true,
				cache: false,
				href: base_url+'daftar_keluarga/add',
				modal: true
			});
			 
			$('#dialog').dialog('open');
			url = base_url+'daftar_keluarga/save';
		}
		// end newData
		
		detailData = function (){
			var row = $('#dg').datagrid('getSelected');
			if (row){
				$('#dialog').dialog({
					title: 'Detail Data Keluarga',
					width: 550,
					height: 550,
					closed: true,
					cache: false,
					href: base_url+'daftar_keluarga/detail/'+row.id_keluarga,
					modal: true
				});
				
				$('#dialog').dialog('open');  
				url = base_url+'daftar_keluarga/detail';
			}
		}
		//end detailData
		
		deleteData = function (val){
			//var row = $('#dg').datagrid('getSelected');
			//if(row){
				if(confirm("Apakah yakin akan menghapus data '" + val + "'?")){
					var response = '';
					$.ajax({ type: "GET",
						 url: base_url+'mahasiswa/delete/' + val,
						 async: false,
						 success : function(response){
							var response = eval('('+response+')');
							if (response.success){
								$.messager.show({
									title: 'Success',
									msg: 'Data Berhasil Dihapus'
								});
								// reload and close tab
								$('#dg').datagrid('reload');
							} else {
								$.messager.show({
									title: 'Error',
									msg: response.msg
								});
							}
						 }
					});
				}
			//}
		}
		//end deleteData 
		
		saveData = function(){
			// get data anggota dari grid
			var str = $('#dg_anggota_keluarga').datagrid('getData');
			$('#anggota_keluarga').val(JSON.stringify(str));

			$('#form1').form('submit',{
				url: url,
				onSubmit: function(){
					return $(this).form('validate');
				},
				success: function(result){
				console.log(result);
				// return 1;
				var result = eval('('+result+')');
				if (result.success){
					$('#dialog').dialog('close');		// close the dialog
					$('#dg').datagrid('reload');		// close the dialog
					//print pdf
					// result.no_surat
					//print_skp(result.no_surat);
				} else {
					$.messager.show({
						title: 'Error',
						msg: result.msg
					});
				}
			}
			});
		}
		//end saveData
		
		/*actionbutton = function(value, row, index){
			var col;
			//if (row.kd_fakultas != null) {
				col = '<a href="#" onclick="detailData(\''+row.id_keluarga+'\');">Detail</a>';
				col += '&nbsp;&nbsp;|&nbsp;&nbsp;<a href="#" onclick="deleteData(\''+row.id_keluarga+'\');">Delete</a>';
			//}
			return col;
		}*/
		
		// load grid
		$(function(){
			$('#dg').datagrid({url:"<?=base_url()?>daftar_surat/grid"});	
			//$('#dg').datagrid('enableFilter'); 			
		});	
		
		
		// filter
		filter = function(){
			$('#dg').datagrid('load',{
				jenis_surat : $('#s_jenis_surat').val(),
				nama_warga : $('#s_nama_warga').val(),
			});
			//$('#dg').datagrid('enableFilter');
		}
		
		//# Tombol Bawah
		$(function(){
			var pager = $('#dg').datagrid().datagrid('getPager');	// get the pager of datagrid
			pager.pagination({
			});			
		});
		
	});
</script>

<table id="dg" title="" data-options="
			rownumbers:true,
			singleSelect:true,
			autoRowHeight:false,
			pagination:true,
			pageSize:30,
			fit:true,
			toolbar:'#toolbar',
			">
	<thead>
		<tr>
			<!--<th field="tahun_penerimaan" sortable="true" width="80">Tahun Penerimaan</th> -->
			<th field="jenis_surat" sortable="true" width="100">Jenis Surat</th>
			<th field="nama_surat" sortable="true" width="200">Nama Surat</th>
			<th field="no_surat" sortable="true" width="150">Nomor Surat</th>
			<th field="nama_warga" sortable="true" width="150">Nama Pemohon</th>			
			<th field="tgl_surat" sortable="true" width="100">Tanggal Surat</th>			
		</tr>
	</thead>
</table>
<div id="toolbar" style="padding:5px;height:auto">
	<div style="margin-bottom:5px">
		
	</div>
	<div>
		<table width="100%" border="0">
		    <tr>
				<td>Jenis Surat</td>
				<td>: 
					<select id="s_jenis_surat" name="s_jenis_surat" style="width:190px;">
						<option value="">-Pilih-</option>
						<option value="SK">Surat Keterangan</option>
						<option value="SKP">Surat Keterangan Pindah</option>
						<option value="SKA">Surat Keterangan Adat</option>
						<option value="SKU">Surat Keterangan Usaha</option>
						<option value="SKD">Surat Keterangan Domisili</option>
						<option value="SL">Surat Kelahiran</option>
						<option value="SM">Surat Kematian</option>
					</select>
				</td>
			</tr>
			<tr>
				<td>Nama Pemohon</td>
				<td>: 
					<input name="s_nama_warga" id="s_nama_warga" size="20">
				</td>
			</tr>	
			<tr>
				<td>&nbsp;</td>
				<td>&nbsp;&nbsp;<a href="#" onclick="filter()" class="easyui-linkbutton btn" iconCls="icon-search">Cari</a></td>
			</tr>
		</table>
	</div>
</div>
